package com.wms.controller;

import com.wms.dto.ConfirmCountDto;
import com.wms.pojo.*;
import com.wms.service.WarehousingEntryDetailsService;
import com.wms.service.WarehousingEntryDetailsSortService;
import com.wms.service.WarehousingEntryUpService;
import com.wms.vo.WarehousingSortVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/warehousing/entry/details/sort")
public class WarehousingEntryDetailsSortController {

    @Autowired
    private WarehousingEntryDetailsSortService sortService;
    @Autowired
    private WarehousingEntryUpService upService;
    @Autowired
    private WarehousingEntryDetailsService detailsService;

    @GetMapping
    public Result queryPage(Integer page, Integer pageSize, String supplierName){
        PageResult pageResult = sortService.queryPage(page,pageSize,supplierName);
        return Result.success(pageResult);
    }

    @PutMapping
    @Transactional
    public Result updateById(@RequestBody WarehousingSortVo vo){
        WarehousingEntryDetailsSort detailsSort = WarehousingEntryDetailsSort.builder().id(vo.getId())
                .count(vo.getTotal())
                .status(2)
                .updateTime(LocalDateTime.now())
                .build();
        sortService.updateById(detailsSort);
        WarehousingEntryDetailsSort sort = sortService.getById(vo.getId());
        //修改详情的状态
        WarehousingEntryDetails details = detailsService.getById(sort.getWarehousingEntryDetailsId());
        details.setUpdateTime(LocalDateTime.now());
        details.setStatus(5);
        detailsService.updateById(details);

        WarehousingEntryUp entryUp = WarehousingEntryUp.builder().detailsSortId(sort.getId())
                .skuId(sort.getSkuId())
                .count(0)
                .status(1)
                .createTime(LocalDateTime.now())
                .updateTime(LocalDateTime.now())
                .build();
        upService.save(entryUp);
        return Result.success();
    }

    @PutMapping("/count")
    @Transactional
    public Result updateByIdAndCount(@RequestBody ConfirmCountDto dto){
        WarehousingEntryDetailsSort sort = sortService.getById(dto.getId());
        //分拣的数量
        Integer count = sort.getCount();
        Integer entryDetailsId = sort.getWarehousingEntryDetailsId();
        WarehousingEntryDetails entryDetails = detailsService.getById(entryDetailsId);
        //分拣的总数
        Integer total = entryDetails.getCount();
        if (dto.getCount() >= total-count){
            WarehousingEntryDetailsSort detailsSort = WarehousingEntryDetailsSort.builder()
                    .id(sort.getId())
                    .count(total)
                    .status(2)
                    .updateTime(LocalDateTime.now())
                    .build();
            sortService.updateById(detailsSort);
            WarehousingEntryUp entryUp = WarehousingEntryUp.builder().detailsSortId(sort.getId())
                    .skuId(sort.getSkuId())
                    .count(0)
                    .status(1)
                    .createTime(LocalDateTime.now())
                    .updateTime(LocalDateTime.now())
                    .build();
            upService.save(entryUp);
            //修改详情的状态
            WarehousingEntryDetails details = detailsService.getById(sort.getWarehousingEntryDetailsId());
            details.setUpdateTime(LocalDateTime.now());
            details.setStatus(5);
            detailsService.updateById(details);
        }else {
            WarehousingEntryDetailsSort detailsSort = WarehousingEntryDetailsSort.builder()
                    .id(sort.getId())
                    .updateTime(LocalDateTime.now())
                    .count(dto.getCount() + count)
                    .build();
            sortService.updateById(detailsSort);
            //修改详情的状态
            WarehousingEntryDetails details = detailsService.getById(sort.getWarehousingEntryDetailsId());
            details.setUpdateTime(LocalDateTime.now());
            details.setStatus(4);
            detailsService.updateById(details);
        }
        return Result.success();
    }
}
    